# @Time : 2025-01-08 10:07
# @Author : Fioman 
# @Phone : 13149920693
"""
设备状态类
"""

import threading
from typing import Dict, Any


class ZmcStateBase:
    """
    设备基础状态类
    """

    def __init__(self):
        self.isOnline = False
        self.isRun = False
        self.isRs = False


class DeviceState:
    """
    设备状态管理类 - 线程安全的单例实现
    """
    _instance = None
    _lock = threading.Lock()

    def __new__(cls):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        if not hasattr(self, '_initialized'):
            # 离线上料控制器状态
            self.offlineZmcState: ZmcStateBase = ZmcStateBase()
            
            # 上料控制器状态
            self.camZmcState: ZmcStateBase = ZmcStateBase()

            # 裁切控制器状态
            self.cbjZmcState: ZmcStateBase = ZmcStateBase()

            # 铡刀控制器状态
            self.cutterZmcState: ZmcStateBase = ZmcStateBase()

            # 工单信息
            self.orderUsedKey = "1"

            self._initialized = True

    def to_dict(self) -> Dict[str, Any]:
        """转换为前端需要的格式"""
        return {
            "offlineZmc": {
                "isOnline": self.offlineZmcState.isOnline,
                "isRun": self.offlineZmcState.isRun,
                "isRs": self.offlineZmcState.isRs
            },
            "camZmc": {
                "isOnline": self.camZmcState.isOnline,
                "isRun": self.camZmcState.isRun,
                "isRs": self.camZmcState.isRs
            },
            "cbjZmc": {
                "isOnline": self.cbjZmcState.isOnline,
                "isRun": self.cbjZmcState.isRun,
                "isRs": self.cbjZmcState.isRs
            },
            "cutterZmc": {
                "isOnline": self.cutterZmcState.isOnline,
                "isRun": self.cutterZmcState.isRun,
                "isRs": self.cutterZmcState.isRs
            },
            "orderUsedKey": self.orderUsedKey
        }
